//
// Calc.cpp for calc 206 in /home/druart_p/Work/math/my-math-epi/206
//
// Made by pierrick druart
// Login   <druart_p@epitech.net>
//
// Started on  Wed May  9 14:45:55 2012 pierrick druart
// Last update Sun Jun 10 20:04:37 2012 pierrick druart
//

#include	"Calc.hh"

// Construtor & Destructor

Calc::Calc(const std::string & file, const std::string n)
{
  std::istringstream iss(n);

  iss >> this->_n;
  this->_individu = 0;
  this->_fileName = file;
  getValueFromFile(file);
  print();
}

Calc::~Calc() {}

// Get File Value

void		Calc::getValueFromFile(const std::string & file)
{
  std::string	line;
  const char	*test = file.c_str();
  std::ifstream myfile(test);
  int		value = 0;

  if (myfile.is_open())
    {
      while (myfile.good())
	{
	  getline(myfile, line);
	  std::istringstream iss(line);
	  iss >> value;
	  if (this->_individu && value)
	    this->_value.push_back(value);
	  else
	    this->_individu = value;
	}
      myfile.close();
    }
}

void		Calc::print()
{
  std::cout << "\tPopulation : " << this->_fileName << std::endl;
  std::cout << "Nombre d'individus : " << this->_individu << std::endl;
  std::cout << "Moyenne : " << moyennePopulation() << std::endl;
  std::cout << "Ecart Type s : " << ecartType() << std::endl << std::endl;
  std::cout << "Echantillon de taille : " << this->_n << std::endl;
  std::cout << "Nombre d'echantillons : " << echantillon() << std::endl;
  std::cout << "Moyenne : " << moyennePopulation() << std::endl;
  std::cout << "Ecart Type : " << ecartType() << std::endl;
  std::cout << "Ecart Type calcule d'apres s : " << ecartType() << std::endl;
}

// Calc

double		Calc::ecartType()
{
  std::list<int>::iterator it;
  double       	res = 0;
  double       	ecart = 0;

  for (it = this->_value.begin(); it != this->_value.end(); ++it)
      res++;
  res = res / this->_individu;
  return (sqrt(res));
}

double	       	Calc::echantillon()
{
  return ((fact(this->_individu)) / (fact(this->_n) * (fact(this->_individu - this->_n))));
}


double	       	Calc::moyennePopulation()
{
  std::list<int>::iterator     	it;
  double			res = 0;

  for (it = this->_value.begin(); it != this->_value.end(); ++it)
    res += *it;
  return (res /= this->_individu);
}

double          Calc::fact(int i)
{
  double	j = 1;

  while (i > 1)
    {
      j *= i;
      i--;
    }
  return (j);
}
